﻿using System;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;

namespace WindowsCompatibility
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                using (var manage = new ManagementClass())
                {
                    manage.Path = new ManagementPath("Win32_Process");
                    foreach (var property in manage.Properties)
                    {
                        Console.WriteLine($"{property.Name}:{property.Value}    {property.Type}     {property.Origin}");
                    }
                }
                Console.WriteLine($"MAC地址：{GetMACString()}");
                Console.WriteLine(GetCpuCount());
                var names = GetCpuName();
                Console.WriteLine("CUP名称:");
                foreach (var name in names)
                {
                    Console.WriteLine(name);
                }

                var ids = GetCpuId();
                Console.WriteLine("CUPID:");
                foreach (var id in ids)
                {
                    Console.WriteLine(id);
                }

                Console.WriteLine($"主板ID：{GetBaseBoardID()}");

                PrintAdaptersInfo();
                PrintCurrentHwProfile();
            }
        }
        /// <summary>
        /// 获取本机的MAC地址
        /// </summary>
        /// <returns></returns>
        public static string GetMACString()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                using (var manage = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    var mo = manage.GetInstances();
                    foreach (ManagementBaseObject m in mo)
                    {
                        Console.WriteLine(m["MacAddress"]?.ToString());
                        if ((bool)m["IpEnabled"] == true)
                        {
                            return m["MacAddress"].ToString();
                        }
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 获取CPU的个数
        /// </summary>
        /// <returns></returns>
        public static int GetCpuCount()
        {
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
                    {
                        ManagementObjectCollection cpus = mCpu.GetInstances();

                        return cpus.Count;
                    }
                }
            }
            catch
            {
            }
            return -1;
        }
        /// <summary>
        /// 获取CPU的名称
        /// </summary>
        /// <returns></returns>
        public static string[] GetCpuName()
        {
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
                    {
                        ManagementObjectCollection cpus = mCpu.GetInstances();
                        var ids = new string[cpus.Count];
                        var index = 0;
                        ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
                        foreach (var mo in mySearch.Get())
                        {
                            ids[index++] = mo.Properties["Name"].Value.ToString();
                            foreach (var property in mo.Properties)
                            {
                                Console.Write($"{property.Name}:{property.Value}    ");
                            }
                            Console.WriteLine();
                            Console.WriteLine("========================================");
                        }
                        return ids;
                    }
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// 获取CPU的ID
        /// </summary>
        /// <returns></returns>
        public static string[] GetCpuId()
        {
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
                    {
                        ManagementObjectCollection cpus = mCpu.GetInstances();
                        var ids = new string[cpus.Count];
                        var index = 0;
                        ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
                        foreach (var mo in mySearch.Get())
                        {
                            ids[index++] = mo.Properties["ProcessorId"].Value.ToString();
                            Console.WriteLine();
                            Console.WriteLine("========================================");
                        }
                        return ids;
                    }
                }
            }
            catch
            {
            }
            return null;
        }

        public static string GetBaseBoardID()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                using (var mBB = new ManagementClass("Win32_BaseBoard"))
                {
                    var moc = mBB.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        return mo.Properties["SerialNumber"].Value.ToString();
                    }
                }
            }
            return string.Empty;
        }

        public static void PrintAdaptersInfo()
        {
            Console.WriteLine("==============================================");
            var list = new AdaptersInfoHelper().GetAdapters();
            var ipAddrType = typeof(IpAddr);
            foreach (var adapter in list.OrderBy(p => p.Index))
            {
                foreach (PropertyInfo propertyInfo in typeof(IpAdapterInfo).GetProperties())
                {
                    if (propertyInfo.PropertyType.Equals(ipAddrType))
                    {
                        var value = (IpAddr)propertyInfo.GetValue(adapter);
                        Console.WriteLine($"{propertyInfo.Name}:    {value.IpAddress}");
                        Console.WriteLine($"{propertyInfo.Name}:    {value.IpMask}");
                    }
                    else if (propertyInfo.PropertyType.Equals(typeof(byte[])))
                    {
                        var value = (byte[])propertyInfo.GetValue(adapter);

                        Console.WriteLine($"{propertyInfo.Name}:  {string.Join('-', value.Select(p => Convert.ToHexString(new byte[] { p })))}");
                    }
                    else
                    {
                        Console.WriteLine($"{propertyInfo.Name}:    {propertyInfo.GetValue(adapter)}");
                    }
                }

                Console.WriteLine("*************************************************************************************************");
            }
        }

        public static void PrintCurrentHwProfile()
        {
            Console.WriteLine("==============================================");

            var data = new CurrentHwProfileHelper().GetCurrentHwProfile();

            Console.WriteLine($"计算机对接状态：{data.dwDockInfo}");
            Console.WriteLine($"当前硬件配置文件的全球唯一标识符：{data.szHwProfileGuid}");
            Console.WriteLine($"当前硬件配置文件的显示名称：{data.szHwProfileName}");
        }
    }
}
